<?php
namespace Controllers;

use DB\FacebookService;
use DB\UserService;

/**
 * @author Evgeny Baranov <evgeny@baranov.eu>
 */
class Profile extends Base {

    protected $defaultView = 'Profile';
    protected $defaultAction = 'edit';

    public function __construct() {
        if (!(\App::getCurrentUser())) {
            \App::error(403, "You are not authorized.");
        }
        parent::__construct();
    }

    public function edit(array $param) {
        $user = \App::getCurrentUser();
        return array('user' => $user);
    }

    public function save(array $param) {
        $this->apply(array());
    }

    public function apply(array $param) {
        $us = new UserService();
        $user = \App::getCurrentUser();
        $user->fromArray($_POST);

        $us->updateUser($user);

        $this->jsonSuccess($user->toArray());
    }

    public function update(array $param) {
        $fb = new FacebookService();
        $profile = $fb->getProfile();

        $us = new UserService();
        $us->updateProfile($profile);
        $this->jsonSuccess($profile->generateUserData()->toArray());
    }
}